package com.aijk.web.admin.utils;

import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by kongqing on 2016/5/13.
 * 身份证
 */
public class IdcardUtil {

    /**
     * 返回格式yyyy-MM-dd
     * @param idcard
     * @return
     */
    public static String getBirch(String idcard){
        String birch = null;
        if(StringUtils.isNotBlank(idcard) && (idcard.length() == 15 || idcard.length() == 18)){
            if(idcard.length() == 15){
                idcard = getEighteenIDCard(idcard);
            }
            birch = idcard.substring(6,14);
            Date time = DateUtil.getDate(birch,"yyyyMMdd",null);
            birch = DateUtil.dateFormate(time,"yyyy-MM-dd");
        }
        return birch;
    }




    /**
     * 获得性别
     * 返回 1男 2女
     * @param idcard
     * @return
     */
    public static Integer getSex(String idcard){
        Integer sex = null;
        if(StringUtils.isNotBlank(idcard) && (idcard.length() == 15 || idcard.length() == 18)){
            if(idcard.length() == 15){
                idcard = getEighteenIDCard(idcard);
            }
            sex = Integer.parseInt(idcard.substring(16,17))%2 == 0 ? 2:1 ;
        }

        return sex;
    }

    /**
     * 返回年龄
     * @param idcard
     * @return
     */
    public static int getAge(String idcard) {
        String birth = getBirch(idcard);

        if (birth == null)
            return 0;
        Date birthDate = DateUtil.getDate(birth);
        int age = 0;
        Date now = new Date();
        SimpleDateFormat format_y = new SimpleDateFormat("yyyy");
        SimpleDateFormat format_M = new SimpleDateFormat("MM");
        String birth_year = format_y.format(birthDate);
        String this_year = format_y.format(now);
        String birth_month = format_M.format(birthDate);
        String this_month = format_M.format(now);
        // 初步，估算
        age = Integer.parseInt(this_year) - Integer.parseInt(birth_year);
        // 如果未到出生月份，则age - 1
        if (this_month.compareTo(birth_month) < 0)
            age -= 1;
        if (age < 0)
            age = 0;
        return age;
    }

    public static void main(String[] args) {
//        String birch = "19870116";
//        Date time = DateUtil.getDate(birch,"yyyyMMdd",null);
//        System.out.println(time);
//        birch = DateUtil.dateFormate(time,"yyyy-MM-dd");
//        System.out.println(birch);
        System.out.println(getEighteenIDCard("370802941031331"));
    }

    public static String getEighteenIDCard(String fifteenIDCard) {
        StringBuilder sb = new StringBuilder();
        if (fifteenIDCard != null && fifteenIDCard.length() == 15) {
            sb.append(fifteenIDCard.substring(0, 6))
                    .append("19")
                    .append(fifteenIDCard.substring(6));
            sb.append(getVerifyCode(sb.toString()));
            return sb.toString();
        }
        return sb.toString();
    }

    public static char getVerifyCode(String idCardNumber){
        char[] Ai = idCardNumber.toCharArray();
        int[] Wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
        char[] verifyCode = {'1','0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
        int S = 0;
        int Y;
        for(int i = 0; i < Wi.length; i++){
            S += (Ai[i] - '0') * Wi[i];
        }
        Y = S % 11;
        return verifyCode[Y];
    }


}
